//
//  LPFStatusCell.swift
//  WeiBo
//
//  Created by mac on 16/4/20.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
class LPFStatusCell: UITableViewCell {
    
    // 记录我们的底部toolBar 的约束
    var toolBarTopConstraint: Constraint?
    
    var status: LPFStatusModel?{
        didSet{
            // 给原创微博赋值
            originalView.status = status
            
            // 在重新给toolbar设置top约束的时候需要干掉以前的约束
            self.toolBarTopConstraint?.uninstall()
            
//            // 没有转发微博
            if status?.retweeted_status == nil{
                
                // 隐藏转发微博(由于cell复用的问题)
                retweetView.hidden = true
                // toolbar 的顶部 ==  原创微博的底部
                toolBarView.snp_makeConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            }else{
                // 有转发微博
                // 显示转发微博(由于cell复用的问题)
                retweetView.hidden = false
                // 设置数据
              retweetView.status = status
                // toobar 的顶部 ==  转发微博的底部
                toolBarView.snp_makeConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }
            
            // 给底部toolbar 设置数据
            toolBarView.status = status
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        contentView.addSubview(originalView)
       contentView.addSubview(retweetView)
        contentView.addSubview(toolBarView)
        
        //添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
//            make.height.equalTo(50)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
         //  make.height.equalTo(100)
//            make.trailing.equalTo(contentView)
        }
//
        toolBarView.snp_makeConstraints { (make) -> Void in
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(37)
        }
        // 给我们contentView 设置约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
        
    }
    
    // MARK: - 懒加载控件
    //原创
    private lazy var originalView: LPFStatusOriginalView = LPFStatusOriginalView()
    //转发
    private lazy var retweetView: LPFStatusRetweetView = LPFStatusRetweetView()
    //底部评论赞
    lazy var toolBarView: LPFStatusToolBarView = LPFStatusToolBarView()

}
